package com.code.leetcode.offer;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/2 19:17
 */
public class The43_M {
    public int countDigitOne(int n) {
        int index = 1, res = 0;
        int height = n / 10, curr = n % 10, low = 0;
        while (height != 0 || curr != 0) {
            if (curr == 0) res += height * index;
            else if (curr == 1) res += height * index + low + 1;
            else res += (height + 1) * index;
            low += curr * index;
            index *= 10;
            curr = height % 10;
            height /= 10;
        }
        return res;
    }
}
